package thread.threadUtil;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author
 * @date 2022年12月01日 20:58
 * @description
 */
public class CountDownLatchDemo {




    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(2);

        long start = System.currentTimeMillis();
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(2000);
                    System.out.println(1);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(1000);
                    System.out.println(2);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        // 一直等待不设置等待时长
        countDownLatch.await();
        // 设置等待时长
        // countDownLatch.await(1000, TimeUnit.MILLISECONDS);

        System.out.println("main:"+(System.currentTimeMillis()-start));
    }
}
